package com.tuanzi.loan.business.repository.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.repository.xhh.settlementsystem.custom.XhhSettlementPrepayRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementPrepayRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementPrepayResponse;
import org.jooq.Condition;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.Date;

import static com.tuanzi.loan.generated.jooq.loan.tables.XhhLoanSettlementPrepay.XHH_LOAN_SETTLEMENT_PREPAY;

/**
 * @author caiwanlin
 * @create 2018-08-22 17:24
 */
public class XhhSettlementPrepayRepositoryImpl extends JOOQBaseRepositoryPlus implements XhhSettlementPrepayRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return XHH_LOAN_SETTLEMENT_PREPAY;
    }

    @Override
    public Page<XhhSettlementPrepayResponse> list(XhhSettlementPrepayRequest searchRequest, Pageable pageable) {
        Long companyId = searchRequest.getCompanyId();
        Integer payMode = searchRequest.getPayMode();
        Integer billStatus = searchRequest.getBillStatus();
        String payAccount = searchRequest.getPayAccount();
        String creator = searchRequest.getCreator();
        Date startTime = searchRequest.getStartTime();
        Date endTime = searchRequest.getEndTime();
        fields(XHH_LOAN_SETTLEMENT_PREPAY.fields());
        eq(XHH_LOAN_SETTLEMENT_PREPAY.COMPANY_ID,companyId);
        eq(XHH_LOAN_SETTLEMENT_PREPAY.CREATOR,creator);
        eq(XHH_LOAN_SETTLEMENT_PREPAY.PAY_ACCOUNT,payAccount);
        eq(XHH_LOAN_SETTLEMENT_PREPAY.PAY_MODE, payMode);
        eq(XHH_LOAN_SETTLEMENT_PREPAY.BILL_STATUS,billStatus);
        between(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME,startTime,endTime);
        orderBy(XHH_LOAN_SETTLEMENT_PREPAY.CREATE_TIME, Sort.Direction.DESC);

        Page<XhhSettlementPrepayResponse> search = search(pageable, record -> {
            XhhSettlementPrepayResponse response = new XhhSettlementPrepayResponse();
            response.setId(record.get(XHH_LOAN_SETTLEMENT_PREPAY.ID));
            response.setCompanyId(record.get(XHH_LOAN_SETTLEMENT_PREPAY.COMPANY_ID));
            response.setCompanyName(record.get(XHH_LOAN_SETTLEMENT_PREPAY.COMPANY_NAME));
            response.setPrepayTime(record.get(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME));
            response.setLastPrepayAmount(record.get(XHH_LOAN_SETTLEMENT_PREPAY.LAST_PREPAY_AMOUNT));
            response.setPrepayAmount(record.get(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_AMOUNT));
            response.setPayMode(record.get(XHH_LOAN_SETTLEMENT_PREPAY.PAY_MODE));
            response.setPayName(record.get(XHH_LOAN_SETTLEMENT_PREPAY.PAY_NAME));
            response.setPayAccount(record.get(XHH_LOAN_SETTLEMENT_PREPAY.PAY_ACCOUNT));
            response.setBillId(record.get(XHH_LOAN_SETTLEMENT_PREPAY.BILL_ID));
            response.setBillStatus(record.get(XHH_LOAN_SETTLEMENT_PREPAY.BILL_STATUS));
            Integer prepayStatus = record.get(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_STATUS, Integer.class);
            response.setPrepayStatus(prepayStatus==null?0:1);
            response.setRemark(record.get(XHH_LOAN_SETTLEMENT_PREPAY.REMARK));
            response.setCreator(record.get(XHH_LOAN_SETTLEMENT_PREPAY.CREATOR));
            response.setCreateTime(record.get(XHH_LOAN_SETTLEMENT_PREPAY.CREATE_TIME));
            response.setUpdator(record.get(XHH_LOAN_SETTLEMENT_PREPAY.UPDATOR));
            response.setUpdateTime(record.get(XHH_LOAN_SETTLEMENT_PREPAY.UPDATE_TIME));
            return response;
        });
        return search;
    }

    @Override
    public Double getAliOrderCount(XhhSettlementPrepayRequest $) {
        Condition condition = XHH_LOAN_SETTLEMENT_PREPAY.ID.isNotNull();
        if($.getCompanyId() != null && $.getCompanyId()!=0){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.COMPANY_ID.eq($.getCompanyId()));
        }
        if($.getCreator()!=null && $.getCreator()!=""){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.CREATOR.eq($.getCreator()));
        }
        if($.getPayAccount()!=null && $.getPayAccount()!=""){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PAY_ACCOUNT.eq($.getPayAccount()));
        }
        condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PAY_MODE.eq(1));
        if($.getBillStatus()!=null){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.BILL_STATUS.eq($.getBillStatus()));
        }
        if($.getStartTime()!=null && $.getEndTime()!=null){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME.ge(new java.sql.Date($.getStartTime().getTime())))
                    .and(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME.le(new java.sql.Date($.getEndTime().getTime())));
        }
        Double into = dslContext.select(DSL.sum(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_AMOUNT)).from(XHH_LOAN_SETTLEMENT_PREPAY)
                .where(condition).fetchOne().into(Double.class);
        return into;
    }

    @Override
    public Double getToTalOrderCount(XhhSettlementPrepayRequest $) {
        Condition condition = XHH_LOAN_SETTLEMENT_PREPAY.ID.isNotNull();
        if($.getCompanyId() != null && $.getCompanyId()!=0){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.COMPANY_ID.eq($.getCompanyId()));
        }
        if($.getCreator()!=null && $.getCreator()!=""){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.CREATOR.eq($.getCreator()));
        }
        if($.getPayAccount()!=null && $.getPayAccount()!=""){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PAY_ACCOUNT.eq($.getPayAccount()));
        }
        if($.getPayMode()!=null){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PAY_MODE.eq($.getPayMode()));
        }
        if($.getBillStatus()!=null){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.BILL_STATUS.eq($.getBillStatus()));
        }
        if($.getStartTime()!=null && $.getEndTime()!=null){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME.ge(new java.sql.Date($.getStartTime().getTime())))
                    .and(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME.le(new java.sql.Date($.getEndTime().getTime())));
        }
        Double into = dslContext.select(DSL.sum(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_AMOUNT)).from(XHH_LOAN_SETTLEMENT_PREPAY)
                .where(condition).fetchOne().into(Double.class);
        return into;
    }

    @Override
    public Double getBankOrderCount(XhhSettlementPrepayRequest $) {
        Condition condition = XHH_LOAN_SETTLEMENT_PREPAY.ID.isNotNull();
        if($.getCompanyId() != null && $.getCompanyId()!=0){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.COMPANY_ID.eq($.getCompanyId()));
        }
        if($.getCreator()!=null && $.getCreator()!=""){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.CREATOR.eq($.getCreator()));
        }
        if($.getPayAccount()!=null && $.getPayAccount()!=""){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PAY_ACCOUNT.eq($.getPayAccount()));
        }
        condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PAY_MODE.eq(2));
        if($.getBillStatus()!=null){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.BILL_STATUS.eq($.getBillStatus()));
        }
        if($.getStartTime()!=null && $.getEndTime()!=null){
            condition = condition.and(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME.ge(new java.sql.Date($.getStartTime().getTime())))
                    .and(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_TIME.le(new java.sql.Date($.getEndTime().getTime())));
        }
        Double into = dslContext.select(DSL.sum(XHH_LOAN_SETTLEMENT_PREPAY.PREPAY_AMOUNT)).from(XHH_LOAN_SETTLEMENT_PREPAY)
                .where(condition).fetchOne().into(Double.class);
        return into;
    }

    @Override
    public Double getWechatOrderCount(XhhSettlementPrepayRequest $) {
        return 0d;
    }
}
